package com.xw.bo.ks.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xw.bo.ks.entity.*;
import com.xw.bo.ks.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author： XW -> ~
 * @create： 2024/3/28 20:05
 */
@Slf4j
@Component
public class KafkaConsumer {

    @Resource
    private OrderService orderService;

    @Resource
    private AnchorInfoService anchorInfoService;

    @Resource
    private GoodsInfoService goodsInfoService;

    @Resource
    private GoodsSkuInfoService goodsSkuInfoService;

    @Resource
    private StoreInfoService storeInfoService;

    @KafkaListener(topics = "myTopic", groupId = "myGroupId107")
    public void listen(String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");

            List<OrderInfo> orderInfoList = JSON.parseArray(jsonArray.toString(), OrderInfo.class);

            log.info("orderInfoList size = {}",orderInfoList.size());
            for (OrderInfo order:orderInfoList) {

                OrderInfo byId = orderService.getById(order.getId());
                if (null == byId){
                    orderService.save(order);
                } else {
                    orderService.updateById(order);
                }
            }
//
        }catch (Exception e){
            e.printStackTrace();
        }

        // 处理接收到的消息
    }


    @KafkaListener(topics = "anchorInfo", groupId = "myGroupId104")
    public void anchorInfo(String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");

            List<AnchorInfo> anchorInfoList = JSON.parseArray(jsonArray.toString(), AnchorInfo.class);

            log.info("orderInfoList size = {}",anchorInfoList.size());
            for (AnchorInfo anchorInfo:anchorInfoList) {

                AnchorInfo byId = anchorInfoService.getById(anchorInfo.getId());
                if (null == byId){
                    anchorInfoService.save(anchorInfo);
                } else {
                    anchorInfoService.updateById(anchorInfo);
                }
            }
//
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @KafkaListener(topics = "goodsInfoKo", groupId = "myGroupId111")
    public void goodsInfo(String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");

            List<GoodsInfoKo> goodsInfoKoList = JSON.parseArray(jsonArray.toString(), GoodsInfoKo.class);

            log.info("goodsInfo size = {}",goodsInfoKoList.size());
            for (GoodsInfoKo goodsInfoKo:goodsInfoKoList) {

                GoodsInfo goodsInfo = new GoodsInfo();
                BeanUtils.copyProperties(goodsInfoKo,goodsInfo);
                goodsInfo.setLevelInfo(goodsInfoKo.getLevel());
                goodsInfo.setStatusInfo(goodsInfoKo.getStatus());
                goodsInfo.setDescInfo(goodsInfoKo.getDesc());

                GoodsInfo byId = goodsInfoService.getById(goodsInfo.getId());
                if (null == byId){
                    goodsInfoService.save(goodsInfo);
                } else {
                    goodsInfoService.updateById(goodsInfo);
                }

                StoreInfo store = goodsInfoKo.getStore();

                StoreInfo storeById = storeInfoService.getById(store.getId());
                if (null == storeById){
                    storeInfoService.save(store);
                } else {
                    storeInfoService.updateById(store);
                }

                List<GoodsSkuInfo> goodsSku = goodsInfoKo.getGoodsSku();
                if (CollectionUtils.isEmpty(goodsSku)){
                    return;
                }

                for (GoodsSkuInfo goodsSkuItem : goodsSku) {
                    GoodsSkuInfo skuById = goodsSkuInfoService.getById(goodsSkuItem.getId());
                    if (null == skuById){
                        goodsSkuInfoService.save(goodsSkuItem);
                    } else {
                        goodsSkuInfoService.updateById(goodsSkuItem);
                    }
                }
            }
//
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
